//
//  LXWaitProcessingViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/7/17.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXWaitProcessingViewController.h"
#import "LXWaitTableViewCell.h"
#import "LXAccurateFindCarViewController.h"
#import "LXCarLeaseApplyViewController.h"
@interface LXWaitProcessingViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger page;
@property (nonatomic) UILabel *tipsLab;
@property (nonatomic) UILabel *tipValueLab;
@end

@implementation LXWaitProcessingViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO,kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self layoutCustomNav:self.view title:@"待办提醒"];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#FAFBFC"];
    
    self.data = [NSMutableArray array];
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(10, kViewTopHeight + 10, ScreenWidth - 20, 62)];
    topView.backgroundColor = [UIColor whiteColor];
    [topView addCorner:4];
    [self.view addSubview:topView];
   
    
    UILabel *tipKeyLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, topView.height)];
    tipKeyLab.text = @"候补车型";
    tipKeyLab.textColor = ColorWith333333;
    tipKeyLab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
    [topView addSubview:tipKeyLab];
    
    UILabel *tipValueLab = [[UILabel alloc] initWithFrame:CGRectMake(topView.width - 115, 0, 100, topView.height)];
    tipValueLab.text = @"0";
    tipValueLab.textAlignment = NSTextAlignmentRight;
    tipValueLab.textColor = ColorWith333333;
    tipValueLab.font = [UIFont systemFontOfSize:26 weight:UIFontWeightMedium];
    self.tipValueLab = tipValueLab;
    [topView addSubview:tipValueLab];
    
    
    
    UILabel *tipsLab = [[UILabel alloc] initWithFrame:CGRectMake(10, topView.Y_height + 10, 100, 22)];
    tipsLab.text = @"补车提醒";
    tipsLab.textAlignment = NSTextAlignmentRight;
    tipsLab.textColor = ColorWith333333;
    tipsLab.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
    [self.view addSubview:tipsLab];
    self.tipsLab = tipsLab;
    
    self.page = 1;
    [self.view addSubview:self.tableView];
    
    [self loadData];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self loadData];
    }];

    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.page++;
        [self loadData];
    }];
    
}

-(void)loadData{
    [MSNetworkManager getRevertUrl:[DOMAIN_URL stringByAppendingString:GET_GetAddCarList] params:@{@"page":@(self.page),@"rows":@(10)} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        [self endRefreshing];
        if (isSuccess) {
            
            self.tipValueLab.text = [NSString stringWithFormat:@"%@",resonseObj[@"total"]];
            
            if ([self.tableView.mj_header isRefreshing]||self.page==1) {
                [self.data removeAllObjects];
                [self.tableView.mj_footer resetNoMoreData];
            }
            
            
            NSArray *list = resonseObj[@"records"];
            [self.data addObjectsFromArray:[CheLiangModel mj_objectArrayWithKeyValuesArray:list]];
            
            NSInteger totalPage = [resonseObj[@"total"] intValue];
            if (self.data.count>=totalPage) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.tableView reloadData];
            
        }
        [self isHaveEmptyData];
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.data.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CheLiangModel *model = self.data[indexPath.section];
//
    LXWaitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXWaitTableViewCell class]) forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.model = model;


    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CheLiangModel *model = self.data[indexPath.section];
    
    
    model.publishedId = model.ID;
    model.ID = model.carBrandId;
    model.carBrand = model.brand;
    model.carMode = model.model;
    
    LXCarLeaseApplyViewController *vc = [[LXCarLeaseApplyViewController alloc] init];
    vc.fromVc = NSStringFromClass([self class]);;
    vc.carModel = model;
    vc.type = 3;
    [self.navigationController pushViewController:vc animated:YES];

    /*
    LXAccurateFindCarViewController *vc = [[LXAccurateFindCarViewController alloc] init];
    model.publishedId = model.ID;
    model.ID = model.carBrandId;
    model.carBrand = model.brand;
    model.carMode = model.model;
    vc.model = model;
    vc.fromVc = NSStringFromClass([self class]);
    [self pushToNextController:vc];
     */
    
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *vv = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
    vv.backgroundColor = self.view.backgroundColor;
    return vv;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, self.tipsLab.Y_height + 5, ScreenWidth, kScreenHeight - self.tipsLab.Y_height - 5) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 10;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#FAFBFC"];
        _tableView.rowHeight = 80;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXWaitTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXWaitTableViewCell class])];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
    }
    return _tableView;
}
- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


@end
